/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bayu.graph;


import com.bayu.common.lib.CommonUtil;
import com.bayu.controller.MainController;
import com.bayu.services.dao.KecelakaanKerjaDao;
import com.bayu.services.dao.RekapKecelakaanKerjaDao;
import com.bayu.services.entity.KecelakaanKerja;
import com.bayu.services.entity.RekapKecelakaanKerja;
import java.awt.Color;
import java.io.OutputStream;
import java.util.List;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 *
 * @author Bayu
 */

@Controller
public class RekapKecelakaanKerjaGraph {
    private RekapKecelakaanKerjaDao dao;
    private KecelakaanKerjaDao kecDao;

    @Autowired
    public void setDao(RekapKecelakaanKerjaDao dao) {
        this.dao = dao;
    }

    @Autowired
    public void setDao(KecelakaanKerjaDao kecDao) {
        this.kecDao = kecDao;
    }

    @RequestMapping("/rekap-kecelakaan-kerja-graph.png")
    public void drawChart(OutputStream stream) throws Exception {
        JFreeChart chart = getChart();
        ChartUtilities.writeChartAsPNG(stream, chart, 1280, 800);
    }

    private JFreeChart getChart() {
        String title="Grafik Kecelakaan Kerja per Tahun ";
        String tahun;


        DefaultCategoryDataset dataset = getDataset();
        final JFreeChart chart = ChartFactory.createBarChart(
            title,
            "",
            "Orang",
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
        );
        chart.setBackgroundPaint(new Color(204, 255, 204));
        CategoryPlot plot = chart.getCategoryPlot();

        plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);

        CategoryItemRenderer renderer = plot.getRenderer();

        renderer.setSeriesPaint(0, Color.blue);
        renderer.setBaseNegativeItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.INSIDE12, TextAnchor.TOP_CENTER));
        renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
        renderer.setBaseItemLabelsVisible(true);

        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setAutoRange(true);
//        rangeAxis.setRange(0, max);
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        return chart;
    }

    private DefaultCategoryDataset getDataset() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        List<RekapKecelakaanKerja> data = this.dao.getAll("from RekapKecelakaanKerja");
        List data2 = this.kecDao.custom("SELECT sum(total) FROM KecelakaanKerja where YEAR(MONTH)="+CommonUtil.getCurrentYear());
        int sekarang = Integer.parseInt(data2.get(0).toString());

        dataset.addValue(data.get(0).getLalu5(), "Total Kecelakaan", "5 Tahun Lalu");
        dataset.addValue(data.get(0).getLalu4(), "Total Kecelakaan", "4 Tahun Lalu");
        dataset.addValue(data.get(0).getLalu3(), "Total Kecelakaan", "3 Tahun Lalu");
        dataset.addValue(data.get(0).getLalu2(), "Total Kecelakaan", "2 Tahun Lalu");
        dataset.addValue(data.get(0).getLalu1(), "Total Kecelakaan", "Tahun Lalu");
        dataset.addValue(sekarang, "Total Kecelakaan", "Tahun Ini");

        
        return dataset;
    }
}
